package cn.xyh.service.impl;


import cn.xyh.dao.RoleWriteMapper;
import cn.xyh.exception.RoleException;
import cn.xyh.model.Role;
import cn.xyh.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleWriteMapper roleWriteMapper;


    /**
     * 新增
     */
    @Override
    public Integer insertRole(Role entity) throws RoleException {

        Integer result = 0;

        try {
            result = roleWriteMapper.insertRole(entity);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除多个
     */
    @Override
    public Integer delRoleByIds(String entityIds) throws RoleException {
        Integer result = 0;
        try {
            String[] _entityIds = entityIds.split(",");
            result = roleWriteMapper.delRoleByIds(_entityIds);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除
     */
    @Override
    public Integer delRole(long id) throws RoleException {
        Integer result = 0;
        try {

            result = roleWriteMapper.delRole(id);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }


    /**
     * 修改
     */
    @Override
    public Integer updateRole(Role entity) throws RoleException {
        int result = 0;

        try {

            result = roleWriteMapper.updateRole(entity);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据ID查找对象
     */
    @Override
    public Role getRoleById(long roleId) throws RoleException {

        Role entity = null;
        try {
            entity = roleWriteMapper.getRoleById(roleId);


        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据Code查找对象
     */
    @Override
    public Role getRoleByCode(String code) throws RoleException {

        Role entity = null;
        try {
            entity = roleWriteMapper.getRoleByCode(code);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据name查找对象
     */
    @Override
    public Role getRoleByName(String name) throws RoleException {

        Role entity = null;
        try {
            entity = roleWriteMapper.getRoleByName(name);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据条件查找列表总数
     */
    @Override
    public Integer getRoleCount(Role entity) throws RoleException {
        int result = 0;
        try {
            result = roleWriteMapper.getRoleCount(entity);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据条件查找列表
     *
     * @param entity      实体对象
     * @param startRownum 开始条数
     * @param endRownum   结束条数
     * @param sortField   排序字段
     * @param sortOrder   正序/倒序
     */
    @Override
    public List<Role> getRoleList(Role entity) throws RoleException {


        List<Role> list = null;

        try {
            list = roleWriteMapper.getRoleList(entity);

        } catch (Exception e) {
            throw new RoleException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return list;
    }

    @Override
    public List<Long> getRoleListById(String currentName) throws RoleException {
        List<Long> list = roleWriteMapper.getRoleListById(currentName);
        return list;
    }

    @Override
    public List<Map<Long, String>> findRoleResourceListByRoleId(Long roleId) throws RoleException {

        List<Map<Long, String>> list = roleWriteMapper.findRoleResourceListByRoleId(roleId);
        return list;

    }

    @Override
    public List<Map<Long, String>> findRolePermissionListByRoleId(Long roleId) throws RoleException {

        List<Map<Long, String>> list = roleWriteMapper.findRolePermissionListByRoleId(roleId);
        return list;

    }
}

